import React, { Component } from 'react'
import { getCurrentInstance } from '@tarojs/taro'
import { getPlatform } from '@UTILS/common'
import { dataFetch } from '@pier88health/p8h-common'
import { setAcquireChannel } from '@UTILS/user'
import Navigate from '@NAVIGATION/navigateManager'

const { isWeb } = getPlatform()

function withCustomTabBarH5(WrappedPage) {
  class WrappedComponent extends Component {
    constructor(props) {
      super(props)
      if (isWeb) {
        this.initThirdWebTabBar()
      }
    }

    async initThirdWebTabBar() {
      const params = dataFetch.getQueryParams(window.location.search)
      if (params.AcquireChannel === 'takeda' || global.IS_TAKEDA_H5) {
        setAcquireChannel('takeda')
        global.IS_TAKEDA_H5 = true
        const items = getCurrentInstance().app.config.tabBar.list
        if (window.location.pathname.indexOf('pages/expert/index') > 0) {
          Navigate.switchTab('homePage')
        } else if (Array.isArray(items) && items.length > 0 && items.length > 3) {
          items.splice(2, 1)
        }
      }
    }

    onLaunch(options) {
      return this.child?.onLaunch?.(options)
    }

    render() {
      return (
        <WrappedPage
          getInstance={
          child => {
            if (child) {
              this.child = child
            }
          }
        }
          {...this.props}
        />
      )
    }
  }
  return WrappedComponent
}

export default withCustomTabBarH5
